package cn.xopencode.appletweb.controller.record.kvoperation;

import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.CommonResult;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.rpc.kvoperation.KvOperationRecordRpc;
import cn.xopencode.recordservice.rpc.kvoperation.dto.*;
import cn.xopencode.recordservice.rpc.kvoperation.vo.*;
import cn.xopencode.recordservice.rpc.robotroom.dto.AdminRecordPageDTO;
import cn.xopencode.userservice.rpc.dept.DeptRpc;
import cn.xopencode.userservice.rpc.user.UserRpc;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "10KV操作室巡检记录 API")
@RestController
@RequestMapping("/kvoperation")
@Validated
public class KvOperationRecordController {

    @DubboReference(version = "${dubbo.consumer.KvOperationRecordRPC.version}")
    private KvOperationRecordRpc kvOperationRecordRpc;

    @DubboReference(version = "${dubbo.consumer.UserRpc.version}")
    private UserRpc userRpc;

    @DubboReference(version = "${dubbo.consumer.DeptRpc.version}")
    private DeptRpc deptRpc;
    /**
     * 根据查询条件分页获取所有的10KV操作室巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllByQuery")
    @ApiOperation("根据查询条件分页获取所有的10KV操作室巡检记录")
    public CommonResult<PageResult<KvOperationRecordPageVO>> getPageKvOperationRecord(KvOperationRecordPageDTO pageDTO){
        CommonResult<PageResult<KvOperationRecordPageVO>> result = kvOperationRecordRpc.pageKvOperationRecord(pageDTO);
        result.getData().getList().forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpRels())){
                pageVO.setEmpRels(userRpc.getEmployeeName(pageVO.getEmpRels()));
            }
        });
        return result;
    }

    /**
     *  新增10KV操作室巡检记录
     * @param roomDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增10KV操作室巡检记录")
    public CommonResult<KvOperationRecordCreateVO> createKvOperationRecord(@Valid KvOperationRecordCreateDTO roomDTO){
        return kvOperationRecordRpc.createKvOperationRecord(roomDTO);
    }

    /**
     *  新增 10KV操作室巡检记录 中的下一组数据
     * @param createDTO
     * @return
     */
    @PostMapping("/addNextLine")
    @ApiOperation("新增 10KV操作室巡检记录 下一组数据")
    public CommonResult<Integer> createKvOperationRecordNextLine(@Valid KvOperationRecordNextCreateDTO createDTO){
        //下一组保存时需要检查是否重复
        return kvOperationRecordRpc.createKvOperationRecordNextLine(createDTO);
    }


    /**
     * 根据id获取10KV操作室巡检记录
     * @param recordId
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据id获取10KV操作室巡检记录")
    public CommonResult<KvOperationRecordVO> getKvOperationRecordById(@RequestParam(required = true) String recordId){
        KvOperationRecordVO recordVO = kvOperationRecordRpc.getKvOperationRecordById(recordId).getData();
        if(recordVO != null){
            recordVO.setEmpNames(userRpc.getEmployeeName(recordVO.getEmpIds()));
        }
        return CommonResult.success(recordVO);
    }

    /**
     * 根据记录标识、下一组的排序，获取10KV操作室巡检记录中下一组的详细
     * @param recordCode
     * @param recordSeq
     * @return
     */
    @GetMapping("/getNextById")
    @ApiOperation("根据记录标识、下一组的排序，获取10KV操作室巡检记录中下一组的详细")
    public CommonResult<KvOperationRecordNextVO> getKvOperationRecordNextById(@RequestParam(required = true) String recordCode, @RequestParam(required = true) Integer recordSeq){
        return kvOperationRecordRpc.getKvOperationRecordNextById(recordCode,recordSeq);
    }

    /**
     *  更新 10KV操作室巡检记录
     * @param updateDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新 10KV操作室巡检记录")
    public CommonResult<Integer> updateKvOperationRecord(@Valid KvOperationRecordUpdateDTO updateDTO){
        return kvOperationRecordRpc.updateKvOperationRecord(updateDTO);
    }

    /**
     *  更新 10KV操作室巡检记录 中的下一组数据
     * @param nextUpdateDTO
     * @return
     */
    @PostMapping("/updateNextLine")
    @ApiOperation("更新10KV操作室巡检记录 下一组数据")
    public CommonResult<Integer> updateKvOperationRecordNextLine(@Valid KvOperationRecordNextUpdateDTO nextUpdateDTO){
        //下一组保存时需要检查是否重复
        return kvOperationRecordRpc.updateKvOperationRecordNext(nextUpdateDTO);
    }

    /**
     * 处理 10KV操作间巡检记录 中的异常，在巡检记录中只是改变状态
     * @param recordDetailId
     * @return
     */
    @PostMapping("/deal")
    @ApiOperation("处理 10KV操作室巡检记录 中的异常")
    public CommonResult<Integer> deal10KvOperationRecordLineException(@RequestParam(required = true) String recordDetailId){
        return kvOperationRecordRpc.dealKvOperationRecordLineException(recordDetailId);
    }


    /**
     * 后台--根据查询条件 分页获取所有的10KV操作室巡检记录
     * @param pageDTO
     * @return
     */
    @PostMapping("/getAllAdminByQuery")
    @ApiOperation("后台--根据查询条件分页获取所有的10KV操作室巡检记录")
    public CommonResult<PageResult<KvOperationRecordAdminPageVO>> getAdminPage10KvOperationRecord(AdminRecordPageDTO pageDTO){
        String level2DeptId = pageDTO.getLevel2DeptId();
        String deptId = pageDTO.getDeptId();
        if(!StringUtils.isEmpty(level2DeptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(level2DeptId).getData());
        }else if(!StringUtils.isEmpty(deptId)){
            pageDTO.setQueryDeptId(deptRpc.getDeptIdList(deptId).getData());
        }
        CommonResult<PageResult<KvOperationRecordAdminPageVO>> result = kvOperationRecordRpc.getAdminPage10KvOperationRecord(pageDTO);
        List<KvOperationRecordAdminPageVO> list = result.getData().getList();
        list.forEach(pageVO -> {
            if(!StringUtils.isEmpty(pageVO.getEmpNames()))
                pageVO.setEmpNames(userRpc.getEmployeeName(pageVO.getEmpNames()));
        });
        return result;
    }
}
